<?php

namespace app\admin\controller;

use app\lib\enum\Status;
use think\Request;

class Institution extends BaseController {


    //机构列表
    public function institutionList() {
        if (request()->isAjax()) {
            $condition = [];
            //收集参数
            $cty_id = input("get.cty_id");
            $ins_name = input("get.ins_name");
            $adm_realname = input("get.adm_realname");
            $page = input("get.page");
            $limit = input("get.limit");
            if ($cty_id != "" && $cty_id != -1) {
                $condition['ins_city_id'] = ['eq', $cty_id];
            }
            if ($ins_name != "") {
                $condition['ins_name'] = ['like', "%" . $ins_name . "%"];
            }
            if ($adm_realname != "") {
                $adminIds = getAdminIdsFromAdminRealname($adm_realname);
                $condition['ins_admin_id'] = ['in', $adminIds];
            }

            //当管理员是培训机构管理员时，只显示他所属的机构信息
            if ($data = isInstitutionAdmin()) {
                $condition['ins_id'] = ['eq', $data['ins_id']];
            }
            $count = count(model("PyInstitution")->where($condition)->select());
            $institutionList = model("PyInstitution")->with("bindAdmin,bindCity")
                ->where($condition)
                ->page($page, $limit)
                ->order("ins_id", "desc")
                ->select();
            foreach ($institutionList as &$value) {
                $value['cty_name'] = $value['bind_city']['cty_name'];
                $value['adm_realname'] = $value['bind_admin']['adm_realname'];
                $value['ins_create_time'] = date("Y-m-d H:i:s", $value['ins_create_time']);
                $value['ins_mobile'] = $value["ins_mobile"] != "" ? $value['ins_mobile'] : "暂无数据";
                $value['ins_location'] = $value["ins_location"] != "" ? $value['ins_location'] : "暂无数据";
            }

            return getJson($count, $institutionList);
        }

        //机构所属区域
        $cityList = model("PyCity")->select();
        $this->assign("cityList", $cityList);
        $this->assign("isInstitutionAdmin", isInstitutionAdmin());
        return view();
    }

    //编辑机构
    public function institutionEdit() {
        $ins_id = input("get.ins_id");
        if (!checkInstitution($ins_id)) {
            $this->error("无权限修改该机构信息");
        }
        if (request()->isAjax()) {
            $data = Request::instance()->param();
            $data['ins_status'] = isset($data['ins_status']) && $data['ins_status'] == "on" ? Status::on : Status::off;
            $data['ins_istop']=isset($data['ins_istop'])&&$data['ins_istop']=="on"?Status::on:Status::off;

            //处理机构所包含课程的信息
            $array = input("post.ins_lessons/a");
            $lessons="";
            if ($array) {
                foreach ($array as $value) {
//                    if (isset($value)) {
//                        $data['ins_lessons'] .= $value . "|";
//                    }
                   $lessons .= $value . "|";
                }
                $data['ins_lessons'] = trim($lessons, "|");
            }
            $result = model("PyInstitution")->edit($data, $ins_id);
            $this->uniteResponse($result, "机构编辑成功");
        }

        //读取机构信息，渲染到页面上
        $institutionInfo = model("PyInstitution")->with("bindAdmin")
            ->find($ins_id);
        $this->assign("institutionInfo", $institutionInfo);
        //读取城市信息
        $cityList = getCityList();
        $this->assign("cityList", $cityList);

        //判断是否已经选择坐标
        $isPosition = true;
        if ($institutionInfo['ins_longitude'] + $institutionInfo['ins_latitude'] + $institutionInfo['ins_zoom'] == 0) {
            $isPosition = false;
        }
        $this->assign("isPosition", $isPosition);

        $this->assign("isInstitutionAdmin", isInstitutionAdmin());


        //读取机构所包含课程的信息
        $institutionHas = explode("|", $institutionInfo['ins_lessons']); //已经包括的课程信息
        $categoryInfo = model("CmsArticleCategory")->where("acc_id", "in", getGoodClassSubCategoryIds())->select();
        $checkBoxList = [];
        foreach ($categoryInfo as $value) {
            $checkBoxList[] = [
                "value" => $value['acc_id'],
                "title" => $value['acc_name'],
                "isCheck" => in_array($value['acc_id'], $institutionHas) ? Status::on : Status::off
            ];
        }
        $this->assign("checkBoxList", $checkBoxList);
        return view();
    }

    //处理图片上传
    public function uploadImg(){
        $file = request()->file('file');
        $src=imgUpload($file,false);
        $result=[
            "code"=>0,
            "mag"=>"上传成功",
            "data"=>[
                "src"=>$src,
            ]
        ];
        return json($result);
    }
}
